<template>
  <div class="information">
    <div class="mod-config">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item  style="margin-top: 80px">
          <el-input v-model="dataForm.name" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item  style="margin-top: 80px">
          <el-input v-model="dataForm.idCard" placeholder="身份证" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <div style="display: flex;margin-bottom: 50px">
            <div style="margin-top: 80px">
              <el-button @click="getDataList()">查询</el-button>
              <el-button  type="primary" @click="addOrUpdateHandle()">新增</el-button>
              <el-button  type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
              <el-button  type="danger" @click="deleteALL()" >一键删除所有字段</el-button>
            </div>
            <el-upload
                class="upload-demo"
                drag
                action="http://andysama.work:8481/information/upload"
                height="150px"
                :on-success="uploadSuccess"
                :on-error="uploadError"
            >
              <i class="el-icon-upload" style="height: 50px;margin: 10px 0"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </div>


        </el-form-item>
      </el-form>
      <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;">
        <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="id"
            header-align="center"
            align="center"
            label="id">
        </el-table-column>
        <el-table-column
            prop="number"
            header-align="center"
            width="250"
            align="center"
            label="考生号">
        </el-table-column>
        <el-table-column
            prop="nickname"
            header-align="center"
            align="center"
            label="姓名">
        </el-table-column>
        <el-table-column
            prop="idCard"
            header-align="center"
            width="280"
            align="center"
            label="身份证号">
        </el-table-column>
        <el-table-column
            prop="sex"
            header-align="center"
            align="center"
            label="性别">
        </el-table-column>
        <el-table-column
            prop="major"
            header-align="center"
            width="180"
            align="center"
            label="专业名称">
        </el-table-column>
        <el-table-column
            prop="courtyard"
            header-align="center"
            align="center"
            label="院系">
        </el-table-column>
        <el-table-column
            prop="specialty"
            header-align="center"
            align="center"
            label="邮政编码">
        </el-table-column>
        <el-table-column
            prop="phone"
            header-align="center"
            width="150"
            align="center"
            label="联系电话">
        </el-table-column>
        <el-table-column
            prop="address"
            header-align="center"
            width="380"
            align="center"
            label="家庭地址">
        </el-table-column>
        <el-table-column
            prop="recipients"
            header-align="center"
            align="center"
            label="收件人">
        </el-table-column>
        <el-table-column
            prop="province"
            header-align="center"
            align="center"
            label="省份">
        </el-table-column>
        <el-table-column
            prop="expressNumber"
            header-align="center"
            width="200"
            align="center"
            label="快递号">
        </el-table-column>
        <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作">
          <template slot-scope="scope">
            <el-button v-show="!(admins.indexOf(scope.row.nickname)>-1)" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button v-show="!(admins.indexOf(scope.row.nickname)>-1)" type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <informationAddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" ></informationAddOrUpdate>
    </div>
  </div>
</template>

<script>
import {informationDeleteAll, informationDetele, informationList, informationUpdate} from "@/request/utils/information";
import informationAddOrUpdate from './information-add-or-update';
export default {
  name: "information",
  components:{
    informationAddOrUpdate
  },
  data() {
    return {
      dataForm: {
        name: '',
        idCard: ''
      },
      informationId: 0,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      admins: ["admin","lvchaofeng"]
    }
  },
  //计算属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true

      informationList({
        'page': this.pageIndex,
        'limit': this.pageSize,
        'name': this.dataForm.name,
        'idCard': this.dataForm.idCard
      }).then(res=>{
        console.log(res)
        if (res && res.code === 0) {
            this.dataList = res.data.list
            this.totalPage = res.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
      })


    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.informationId = id
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        informationDetele(ids).then(data => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
    uploadSuccess(response, file, fileList)	{
      this.$message({
        message: '上传成功',
        type: 'success',
        duration: 1500,
        onClose: () => {
          this.getDataList()
        }
      })
    },
    uploadError(err, file, fileList){
      this.$message.error("文件上传失败请联系管理员")
    },

    clickisgo(item,status){

      item.isGo = status;
      informationUpdate(item).then(res=>{
        if(res.code===0){
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
            }
          })
        }
      })
    },
    deleteALL(){

      this.$confirm(`确定要全部删除嘛，我将会保留admin用户其余用户将全部删除`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        informationDeleteAll().then(data => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })

    }
  },
  //生命周期 一 创建完成(可以访问当前this实例)
  created() {
    this.getDataList();
  },
  //生命周期 一 挂载完成(可以访问DOM元素)
  mounted() {

  },
  //生命周期 - 创建之前
  beforeCreate() {
  },
  //生命周期 - 挂载之前
  beforeMount() {
  },
  //生命周期 - 更新之前
  beforeUpdate() {
  },
  //生命周期 - 更新之后
  updated() {
  },
  //生命周期 - 销毁之前
  beforeDestroy() {
  },
  //生命周期 - 销毁完成
  destroyed() {
  },
  //如果页面有keep-alive缓存功能，这个函数会触发
  activated() {
  },
}
</script>

<style scoped>
.upload-demo{
  margin-left: 500px;
  height: 150px;
}
.upload-demo>div{
  height: 150px;
}
.el-upload-dragger{
  height: 150px;
}
</style>